/************************************************************************************
* YampManager.cpp                                                                   *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "YampManager.h"
#include "common/SizeConstants.h"

#include <QVBoxLayout>

YampManager::YampManager(QWidget *parent) :
    QWidget(parent) {
    m_components = new QHash<QString, YampComponent*>;
    m_wstack     = new QStackedWidget(this);
    m_listWidget = new QListWidget(this);
    m_nameIndexMap = new QHash<QString, int>;
    m_toggleViz   = new OrientationButton("<Hide<", this);
    m_showList = true;
    setupUi();
    setupActions();
    setupConnections();
}


YampManager::~YampManager() {
    foreach(YampComponent* comp, m_components->values()) {
       m_wstack->removeWidget(comp);
       delete comp;
    }
    m_components->clear();
    delete m_components;
    delete m_wstack;
    delete m_listWidget;
    delete m_nameIndexMap;
    delete m_toggleViz;
}




void YampManager::setupUi() {


    QHBoxLayout* layout = new QHBoxLayout();
    QSplitter *splitter = new QSplitter(this);

    m_listWidget->setObjectName("ywidget");
    m_listWidget->setAlternatingRowColors(true);
    m_toggleViz->setOrientation(Qt::Vertical);
    m_toggleViz->setMaximumWidth(20);
    m_toggleViz->setObjectName("ywidget");


    QWidget *widget = new QWidget(this);
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(m_toggleViz);
    mainLayout->addWidget(m_wstack);
    widget->setLayout(mainLayout);

    splitter->addWidget(m_listWidget);
    splitter->addWidget(widget);
    splitter->setSizes(QList<int>() << 10 <<400);
    layout->addWidget(splitter);
    setLayout(layout);
}

void YampManager::setupActions() {

}

void YampManager::setupConnections() {
    connect(m_listWidget, SIGNAL(currentRowChanged(int)), m_wstack, SLOT(setCurrentIndex(int)));
    connect(m_toggleViz, SIGNAL(clicked()), this, SLOT(setListVisibility()));
}


void YampManager::addComponent(YampComponent* comp) {
    comp->setObjectName("ywidget");
    QObjectList chObj = comp->children();
    foreach(QObject* obj, chObj) {
        if(obj->isWidgetType()) {
            QWidget* widget = qobject_cast<QWidget*>(obj);
            widget->setObjectName("ywidget");
        }
    }
    m_components->insert(comp->name(), comp);
    m_wstack->addWidget(comp);
    QListWidgetItem *item;
    if(comp->icon()) {
        item = new QListWidgetItem(*comp->icon(), comp->name());
    }
    else {
        item = new QListWidgetItem(comp->name());
    }
    m_nameIndexMap->insert(comp->name(), m_components->size() - 1);
    m_listWidget->addItem(item);
    m_wstack->setCurrentIndex(0);
}

void YampManager::removeComponent(QString componentName) {
    if(m_components->contains(componentName)) {
        YampComponent* comp = m_components->value(componentName);
        QListWidgetItem *item = m_listWidget->item(m_nameIndexMap->value(componentName));
        m_wstack->removeWidget(comp);
        m_listWidget->removeItemWidget(item);
        delete m_components->value(componentName);
        m_components->remove(componentName);
        delete item;
    }
}

YampComponent* YampManager::component(QString componentName) {
    if(m_components->contains(componentName)) {
        return m_components->value(componentName);
    }
    return NULL;
}

QList<YampComponent*> YampManager::getAllComponents() {
    return m_components->values();
}

void YampManager::indexSelected(QModelIndex index) {
    int row = index.row();
    m_wstack->setCurrentIndex(row);
}


void YampManager::setListVisibility() {
    m_showList = !m_showList;
    m_toggleViz->setText(m_showList?tr("Hide List"):tr("Show List"));
    m_listWidget->setVisible(m_showList);
}
